
* Converting to spmatrix format : based on spatial_3_*


	** What this file does: 
		 
	** a) PLACEHOLDER, can be run for different matrices to save spatial objects
	** b) ACTIVATOR, calls up existing matrices (needed for creation of spatial lag)
	** c) CONNECTOR, puts the product space together with the spatial variable 
	
	** Contents: robustness check on matrix overlaps
	
	** 1) Extract vertical matrix from product space 
	** 2) Merge with other matrices to identify overlap 
	** 3) Create disjoint alternatives for all specifications 
	** 4) Transform to spatial lag variable in main regression data 

capture log close 
clear all
set more off 
set maxvar 30000, permanently // required to load matrix 

* Set directory
global server "R:\WSV2\TBu_AKe\Spatial_NEW" 
cd "$server" // set working directory 

capture mkdir Grid_overlap_1602
global store "${server}\Grid_overlap_1602"

*Define sub directories
global desktop "C:\Users\hy65byfe\Desktop\smerge_0712"
*cd "$desktop" // set working directory 

cd "$store"
log using spatial_4_spgen_overlap_grid_1702, replace 

global matrix "R:\WSV2\TBu_AKe\Spatial_NEW\Grid_4950" 
cd "$matrix" // set working directory 



cd // display in log file 	
	

	*************************************
	** 1) W matrix, save vertical only **
	*************************************

use spatial_3_wmat_grid_polar 


keep cell a_bin r_* // reduce 

rename r_* v_* // rename the vertical matrix 

preserve 

keep cell a_bin v* 

save "$store\wmat_vert_grid.dta", replace 

restore	

clear 


		*******************************
		****  2) Load Grid Sections ***
		*******************************
		
cd "$store"
		

forvalues q = 5(5)40 {
	
	use "R:\WSV2\TBu_AKe\Spatial_NEW\Grid_4950\spatial_3_wmat_grid_`q'.dta" 
	
	keep cell o* w* 
	merge 1:1 cell using wmat_vert_grid 

		forvalues j = 1/4950 {
		
			capture quietly replace w`q'_`j' = 0 if v_`j' == 1
			capture quietly replace o`q'_`j' = 0 if v_`j' == 1
	
	}
	
	xtset cell 
	spset cell // must by spset. 

	cd "$store"

			*** O
	spmatrix fromdata O`q' = o`q'_1 - o`q'_4950, normalize(none) 

	spmatrix save O`q' using O`q'.stswm, replace // inverse: spmatrix use

			*** W
	spmatrix fromdata W`q' = w`q'_1 - w`q'_4950, normalize(none) 

	spmatrix save W`q' using W`q'.stswm, replace // inverse: spmatrix use

	clear 
		
}

		*** add the horizontal matrix *** 
	use "R:\WSV2\TBu_AKe\Spatial_NEW\Grid_4950\spatial_3_wmat_grid_polar.dta" 
	
	keep cell r* h* 
	rename r_* v_* // rename the vertical matrix 
	*merge 1:1 cell using wmat_vert_grid // not needed, same dataset 

		forvalues j = 1/4950 {
		
			capture quietly replace h_`j' = 0 if v_`j' == 1
	
	}
	
	xtset cell 
	spset cell // must by spset. 

	cd "$store"

			*** Rook
	spmatrix fromdata R = v_1 - v_4950, normalize(none) 

	spmatrix save R using R.stswm, replace // inverse: spmatrix use

			*** Horizontal
	spmatrix fromdata H = h_1 - h_4950, normalize(none) 

	spmatrix save H using H.stswm, replace // inverse: spmatrix use

	clear 
	capture log close 


	**************
log using spatial_4_spgen_overlap_grid_1702, append 
		
		***************************
		*** Activate all versions *
		***************************
/* 		
*cd "$store"
cd "R:\WSV2\TBu_AKe\Spatial_NEW\Grid_overlap_1602"

forvalues q = 5(5)40 {
	
	capture spmatrix use W`q' using W`q'.stswm, replace	
	capture spmatrix use O`q' using W`q'.stswm, replace	

 }
 
capture spmatrix use H using H.stswm, replace // horizontal 
capture spmatrix use R using R.stswm, replace // rook (vertical)

spmatrix dir // display active matrices  

*/ 

 
		************************************************
		* 4) Transform weight matrix to Finished Data **
		************************************************

		**********************
		**** monthly data *****
		**********************

*cd "R:\WSV2\TBu_AKe\Spatial_NEW"
cd "C:\Users\hy65byfe\Desktop\smerge_0712"
use spatial_6_regs_s_monthly, replace

drop if missing(date) // 485,100 obs initially, already cleared here. 
 
*** spset 
xtset cell kt // kt: cntry-year observation: must be balanced. 
spset cell  // set identifier
		
		
		
		
*******************************************************************
	 ** basic reg: index: i = product, k = cell, var kt = country-year pairs
		* HERE: n
		* log_x_k,t = wx_k,t-1 + log_x_k,t-1 + e_i,t(clustered)
********************************************************************
 
			***************************
			** Fill in the blanks *****
			***************************

******** Outcome: sales 
		** lag, 
*codebook L3_sd // 9k missing
replace L3_sd = 0 if missing(L3_sd) // assume: missing is a zero. probably reasonable. 
replace L3_lsd = 0 if missing(L3_lsd)  // assume: missing as true missing or missing as zero are actually 1 single units. not reasonable. 

		** contemp. 
*codebook log_sd
replace log_sd = 0 if missing(log_sd) // 286k (all except the )

******** Outcome: count 
		** lag
		
*codebook L3_cd // 9k missing
replace L3_cd = 0 if missing(L3_cd) // assume: missing is a zero. probably reasonable. 
replace L3_lcd = 0 if missing(L3_lcd)  // assume: missing as true missing or missing as zero are actually 1 single units. not reasonable. 
		
		** contemp. 
*codebook log_cd
replace log_cd = 0 if missing(log_cd)


******** Load Matrices
			
spmatrix dir // check active matrices. MANUAL: if cleared all must activate with spmatrix use. 

cd "$store"

forvalues q = 5(5)40 {
	
	capture spmatrix use W`q' using W`q'.stswm, replace	
	capture spmatrix use O`q' using O`q'.stswm, replace	

 }
 
capture spmatrix use H using H.stswm, replace // horizontal 
capture spmatrix use R using R.stswm, replace // rook (vertical)


spmatrix dir

******** Gen Lags for outcome variables

	
 forvalues q = 5(5)40 {
	gen W`q'_L3_sd = 0 
	gen W`q'_L3_cd = 0 
	
	
	gen O`q'_L3_sd = 0 
	gen O`q'_L3_cd = 0 
}
 

 ***  H and R  
	gen H_L3_sd = 0 
	gen H_L3_cd = 0 

	gen R_L3_sd = 0 
	gen R_L3_cd = 0 
	

keep if year == 2014 // recall: 'time' is country in the spstructure. 

		* loop logic is following: go through 4 outcome vars. 
			* for each one, go country by country, create auxiliary spgen
			* then combine all into 1 var. drop auxiliaries 


egen k14 = group(ccode date) // with monthly data, repeated values in year. new identifier keeps spgen running. 	
/* 
browse if missing(k14)
tab month if missing(k14)
*/ 

drop if missing(date) // 34,650, all cleared here already 
	
sum k14, meanonly  // date ccode group. 
scalar kmin = `r(min)' // store in scalars if sub-sample used later. 
scalar kmax = `r(max)'
	
scalar list kmin kmax // 1 to 84 
 
 *** spset 
xtset cell k14 // kt: cntry-year observation: must be balanced.
capture spset cell  // set identifier

** CHECKPOINT: if this works, the data is set up correctly.

 
 ***  H and R  
 
foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = H*`var' if k14==`k'
         quietly replace H_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = R*`var' if k14==`k'
         quietly replace R_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
      } 
} 
 

 *** All other values
 
forvalues q = 5(5)40 {
 
	foreach var of varlist L3_sd L3_cd {
		
		forvalues i = `=kmin'/`=kmax' {
		local k = `i' 
		
		spgenerate Waux_`var'_`i' = W`q'*`var' if k14==`k'
         quietly replace W`q'_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = O`q'*`var' if k14==`k'
         quietly replace O`q'_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
         } 
	}
 } 
 
 
browse cell year ccode H* R* W* O* L3_sd L3_cd if cell <= 50 & year == 2014 // looks ok? 

save spatial_regs_s_monthly_grid_1702_overlap, replace


clear 